import { defineStore } from "pinia";
import type { RouteLocationNormalized } from "vue-router";

export type OpenedTab = {
    menuId: number;
    tabId: string;
    tabName: string;
    route: RouteLocationNormalized;
};

type TabState = {
    openedTabs: OpenedTab[];
    activeTabId: string;
    activeMenuId: number;
}
export const useTabStore = defineStore("tab", {
    state: (): TabState => {
        return {
            activeTabId: "",
            openedTabs: [],
            activeMenuId: -1,
        }
    },
    actions: {
        addTab(tab: OpenedTab) {
            const idx = this.openedTabs.findIndex(it => it.tabId == tab.tabId);
            if (idx == -1) {
                this.openedTabs.push(tab);
            }
        },
        removeTab(tab: OpenedTab) {
            const idx = this.openedTabs.findIndex(it => it.tabId == tab.tabId);
            if (idx != -1) {
                this.openedTabs.splice(idx, 1);
            }
        },
        clear() {
            this.activeTabId = "";
            this.openedTabs = [];
        }
    },
});